package com.googlecode.mailnet.simulation;

import com.googlecode.mailnet.info.ISimulationInfo;
import com.googlecode.mailnet.netmodel.NetworkConfiguration;

/**
 * @author Iskander Akishev
 */
public class NetworkSimulator {
//	private final SimulationStatistics simulationStatistics;
	private final SimulationState simulationState;
	private final int numberOfSteps;
	private final Thread thread;

	public NetworkSimulator(NetworkConfiguration networkConfiguration, int numberOfSteps) {
		networkConfiguration.confirm();
//		this.simulationStatistics = new SimulationStatistics(networkConfiguration);
		this.simulationState = new SimulationState(networkConfiguration);
		this.numberOfSteps = numberOfSteps;
		thread = new Thread() {
			@Override
			public void run() {
				while (!interrupted()) {
					makeSimulationStep();
				}
			}
		};
	}

	public void start() {
		if (thread.getState() != Thread.State.NEW) {
			throw new IllegalStateException("The simulator was already started");
		}
		thread.start();
	}

	public void stop() {
		thread.interrupt();
	}

	/**
	 * Performs a single simulation step.
	 */
	private void makeSimulationStep() {
//		if (simulationStatistics.getNumberOfSteps() >= numberOfSteps) {
//			stop();
//			return;
//		}
		simulationState.makeStep();
	}

	/**
	 * TODO provide full-featured process control interface
	 */
	public void makeStep() {
		simulationState.makeStep();
	}
	
	/**
	 * Returns the general information provider.
	 * @return Information provider.
	 */
	public ISimulationInfo getInfo() {
		return simulationState;
	}
}
